package tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

//import junit.framework.Assert;
import org.testng.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import static org.testng.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SearchMovie extends tests.supers.TestBaseOld {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testSearchMovie() throws Exception {
    driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("admin");
    driver.findElement(By.name("submit")).click();
//to find all movies
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys(Keys.RETURN);

    explicitWait(By.cssSelector("div.title"));

    String randomMovieToSearch="";
    String searchResult="";

    try{
    	randomMovieToSearch = driver.findElement(By.cssSelector("div.title")).getText();

    	driver.findElement(By.id("q")).clear();
        driver.findElement(By.id("q")).sendKeys(randomMovieToSearch + Keys.RETURN);

        explicitWait(By.cssSelector("div.title"));

        searchResult = driver.findElement(By.cssSelector("div.title")).getText();

    	Assert.assertTrue(searchResult.contains(randomMovieToSearch));
    	System.out.println("Search works well");
    }
    catch(Exception e){
//check if there are movies in DB at all
    	if (isElementPresent(By.cssSelector("div#content")))
    		searchResult=driver.findElement(By.cssSelector("div#content")).getText();

    	driver.findElement(By.linkText("Log out")).click();
        assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));

        if(searchResult.contains("No movies where found."))
        {
        	Assert.fail("No movies to search");
        }
        else{
        	Assert.fail("Search failed.");
    	}
    }

    driver.findElement(By.linkText("Log out")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
  }



  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private void explicitWait(By locator)
  {
	  for (int count = 0;; count ++) {
		  try{ Thread.sleep(1000);}catch (InterruptedException ie) {}
      if (count >= 3)
          return;
      try {
          driver.findElement(locator);
          return;
      } catch (NoSuchElementException e) { }

	  }//for
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

